﻿namespace UnitFundProfitability.BusinessLogic
{
	using System;
	using System.Deployment.Application;
	using UnitFundProfitability.DataMigration;

	/// <summary>
	/// Class which manages software updates.
	/// </summary>
	public static class UpdatesManager
	{
		#region Events
		/// <summary>
		/// Occurs when updating starts.
		/// </summary>
		public static event EventHandler Updating;

		/// <summary>
		/// Occurs when updating is finished.
		/// </summary>
		public static event EventHandler Updated;
		#endregion

		#region Methods
		/// <summary>
		/// Ensures the latest data.
		/// </summary>
		public static void EnsureLatestData()
		{
			if (ApplicationDeployment.IsNetworkDeployed)
			{
				if (ApplicationDeployment.CurrentDeployment.IsFirstRun)
				{
					if (Updating != null)
					{
						Updating(typeof(UpdatesManager), EventArgs.Empty);
					}
					using (UnitFundsEntities dataContext = new UnitFundsEntities())
					{
						DataMigrationHelper.Migrate(dataContext.Connection.DataSource);
					}
					if (Updated != null)
					{
						Updated(typeof(UpdatesManager), EventArgs.Empty);
					}
				}
			}
		}
		#endregion
	}
}
